package com.xiaozhengkeji.starbootserver.api.system;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.xiaozhengkeji.starbootserver.base.redis.VerifyBaseRedis;
import com.xiaozhengkeji.starbootserver.bean.ResponseData;
import com.xiaozhengkeji.starbootserver.bean.ResponseType;
import com.xiaozhengkeji.starbootserver.events.SystemUserRegisterEvent;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 系统公共服务Api
 */
@Slf4j
@RestController
@RequestMapping("/web/system/public/service")
public class SystemPublicServiceApi {
    public VerifyBaseRedis verifyBaseRedis;
    private final ApplicationEventPublisher publisher;

    @Autowired
    public SystemPublicServiceApi(VerifyBaseRedis verifyBaseRedis, ApplicationEventPublisher publisher) {
        this.verifyBaseRedis = verifyBaseRedis;
        this.publisher = publisher;
    }

    /**
     * 生成验证码
     */
    @ApiOperation("生成验证码")
    @GetMapping("generateVerificationCode")
    public void generateVerificationCode(@ApiParam("当前毫秒的时间戳 例:1630231441052") @RequestParam("date") String date,
                                         HttpServletResponse response) {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 50);
        /*将此验证码Tap的正确验证码写入Redis,并设置60秒缓存*/
        verifyBaseRedis.setVerificationCode(date, lineCaptcha.getCode());
        try {
            lineCaptcha.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送邮件验证码
     */
    @ApiOperation("发送邮件验证码")
    @GetMapping("sendEmailVerificationCode")
    public ResponseData sendEmailVerificationCode(@ApiParam("邮箱") @RequestParam("mailbox") String mailbox,
                                                  @ApiParam("用户名") @RequestParam("username") String username,
                                                  @ApiParam("获取图片验证码时提交的时间戳") @RequestParam("vt") String vt,
                                                  @ApiParam("图片验证码的验证码") @RequestParam("vc") String vc) {
        /*验证验证码是否正确*/
        if (verifyBaseRedis.hasVCode(vt, vc)) {
            /*发送邮件验证码*/
            //向观察者发送事件异步发送邮件验证码
            publisher.publishEvent(new SystemUserRegisterEvent(this, mailbox, username, true));
            return new ResponseData(ResponseType.SUCCESS, "发送邮件验证码成功", null, null);
        } else {
            if (verifyBaseRedis.getVerificationCode(vt) == null) {
                return new ResponseData(ResponseType.FAIL, "验证码已过期", null, null);
            } else {
                return new ResponseData(ResponseType.FAIL, "验证码错误", null, null);
            }
        }
    }
}
